import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
import './App.scss'

// 导入页面组件
import { Login } from '@/pages/Login.js'
import { GeekLayout } from '@/pages/GeekLayout/GeekLayout'
import { NotFound } from '@/pages/NotFound'
import { Button } from 'antd'
import { Redirect } from 'react-router-dom'
import { isAuth } from './utils'
function App() {
  return (
    <Router>
      <div className="App">
        <Switch>
          {/* 添加路由重定向 */}
          <Route exact path="/" render={() => <Redirect to="/home"></Redirect>} />
          {/* <Route path="/home" component={GeekLayout} /> */}
          <Route
            path={'/home'}
            render={props => {
              // console.log('home 组件的 props=>', props)
              // console.log('home 组件的 props=>', props.history.location === props.location)
              if (isAuth()) {
                // 用户登录则返回 home 组件
                return <GeekLayout />
              } else {
                // 用户未登录则返回 Redirect 重定向到 login
                return (
                  <Redirect
                    to={{
                      pathname: '/login', // 重定向后要去的页面
                      state: {
                        // 重定向前的页面相关信息
                        from: props.location.pathname
                      }
                    }}
                  />
                )
              }
            }}
          />
          <Route path="/login" component={Login} />
          <Route component={NotFound} />
        </Switch>
      </div>
    </Router>
  )
}

export default App
